class IndexController < ApplicationController
  caches_page :index,:method_missing
  
  def index
    @user = User.count
    @company = Company.count
    @incumbent = Incumbent.count
    @keywords = Keyword::SearchWord.find(:all,:order => "search_times DESC",:limit => 5)
  end
  
  def method_missing(name, *args)
    render :template=>"index/null"
  end
  
  def post_tag_notice
    render :inline=>%{&nbsp;<script type="text/javascript">openAlert('标签可以标记你提供的信息，其他用户可以通过标签方便地找到这些信息。',400,50);</script>}
  end
  
  def post_site_notice
    render :inline=>%{&nbsp;<script type="text/javascript">openAlert('薪情在线网站仅仅是一个提供用户分析以及分享个人薪资状况的平台，对于网站中用户发布的数据，评论以及相关信息带来的影响，薪情在线网站不承担责任。网站用户是其提供信息的权利人，未经用户许可，任何机构或个人均不得为商业目的（含潜在商业目的）使用网站中用户发布的信息。同时，用户有权利隐去个人信息，网站有义务尽最大限度保护匿名的用户信息。',350,135);</script>}
  end
  
  def post_evaluate_notice
    render :inline=>%{&nbsp;<script type="text/javascript">openAlert('可信度表明了数据的好坏的程度，其中红色为经过统计分析后的结果，绿色为网站用户的评估投票后的结果，相应的数值越高，数据越为可信。',260,100);</script>}
  end
  
  def post_grade_notice
    render :inline=>%{&nbsp;<script type="text/javascript">openAlert('通过将该职位级别除以公司级别得到的值，相应的值越高，说明该数据的职位在公司中级别越高。',230,100);</script>}
  end
  
  def post_picture
    picture = Picture.find(params[:id])
    send_data(picture.data,
              :filename=>picture.name,
              :type =>picture.content_type,
              :disposition=>"inline")
  end
  
  def post_validation
    picture=Validation.new(params[:filename])
    session[:code]=picture.code
    send_data(picture.data,
              :filename=>picture.filename,
              :type =>picture.type,
              :disposition=>"inline")
  end
  
  def post_position
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @positions = Position.find(:all,:conditions=>["function_id=?",request.raw_post]).map {|u| [u.name, u.id] }
      render :inline=>%{<%= select 'incumbent', 'tposition_id',@positions%>}
    else
	  render :text=>"请选择所属职能"
    end
  end
  
  def post_city
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @cities = City.find(:all,:conditions=>["region_id=?",request.raw_post]).map {|u| [u.name, u.id] }
      render :inline=>%{<%= select 'incumbent', 'tcity_id',@cities%>}
    else
      render :text=>"请选择所在地区"
    end
  end
  
  def post_rank
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @i = request.raw_post.to_i
      render :inline=>%{<%= select 'incumbent', 'trank',1..@i%>}
    else
      render :text=>"请输入一个有效的所在公司的内部级别。（整数型，数字越大，级别越高）"
    end
  end
  
 # def post_company_rank
 #   if request.raw_post.to_i.integer? && request.raw_post.to_i>0
 #     id = request.raw_post.to_i
 #     @company=Company.find_by_id(id)
 #     render :inline=>%{<%= select 'incumbent', 'trank',1..@company.grade%>}
 #   else
 #     render :text=>"请输入一个有效的所在公司的内部级别。（整数型，数字越大，级别越高）"
 #   end
 # end
  
 # def post_company_info
 #   id = request.raw_post.to_i
 #   if id==0
 #     render :partial=>"report/create_company"
 #   else
 #     @company=Company.find_by_id(id)
 #     render :partial=>"report/company_info"
 #   end
 # end
  
  def post_company_all
    id = params[:id].to_i
    if id==0
      render :update do |page|
        page.replace_html :infoList,:partial=>"report/create_company"
        page.replace_html :rankList,:inline=>"请输入一个有效的所在公司的内部级别。（整数型，数字越大，级别越高）"
        page.replace :company_id, :inline=>"<input id='company_id' name='company_id' type='hidden' value=0 />"
      end
    else
      @company=Company.find_by_id(id)
      render :update do |page|
        page.replace_html :infoList,:partial=>"report/company_info"
        page.replace_html :rankList,:inline=>%{<%= select 'incumbent', 'trank',1..@company.grade%>}
        page.replace :company_id, :inline=>"<input id='company_id' name='company_id' type='hidden' value="+@company.id.to_s+" />"
      end
    end
  end
  
  def post_company
    company_name = params[:company_name].strip
    additional_conditions="industry_id ="+ params[:company_tindustry_id] + " and entity_id ="+params[:company_tentity_id]+" and deleted=0"
    query_conditions=Keyword.create_ajax_conditions(company_name,3,additional_conditions)      
    @companies=Company.find(:all,:conditions =>query_conditions,:order => "view_times DESC",:limit => 4)
    render :partial => "/report/auto_companies_list"
  end
  
  def post_company_list
    @companies=Company.find(:all,
                            :conditions => ["deleted=0 and industry_id = ? and entity_id = ?" , params[:company_tindustry_id], params[:company_tentity_id]],
                            :order => "view_times DESC")
    render :update do |page|
      page.replace_html "infoList",:partial=>"report/companies_list"
      page.replace "company_id",:inline=>"<input id='company_id' name='company_id' type='hidden' value=0 />"
    end
  end
  
  def listposition
    id = request.raw_post.to_i
    @positions=Position.find(:all,:conditions=>["function_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'position', 'id',@positions,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
  def listcompany
    id = request.raw_post.to_i
    @companies=Company.find(:all,:conditions=>["industry_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'company', 'id',@companies,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
  def listcity
    id = request.raw_post.to_i
    @cities=City.find(:all,:conditions=>["region_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'city', 'id',@cities,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
end
